<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Upload;
use Think\Page;
class IndexController extends CommonController {
    // 主页
    public function index(){
        //echo 123;die;
        $this->display();
    }

    //添加用户
    public function adduser()
    {
        if(IS_POST){
            $param['register_name']=I('register_name');
            $param['tel'] = I('tel');//手机号
            $param['agent_tel'] = I('agent_tel');
            $param['password'] = md5(I("password"));//密码
			$param['passwordTwo'] = I("password");//密码
            $param["create_time"]=date('y-m-d h:i:s',time());
            if($param['agent_tel']){
                $agent=M("register")->where("tel=".$param["agent_tel"])->find();//上一级用户信息
                if($agent){
                    $agent_id = $agent['id'];//上一级id
                    $upper_agent_id = $agent['agent_id'];//上上级id
                    $upper_upper_agent_id = $agent['upper_agent_id'];//上上上级id
                    $agent_tel = $agent['tel'];//上一级id
                    $upper_agent_tel = $agent['agent_tel'];//上上级id
                    $upper_upper_agent_tel = $agent['upper_agent_tel'];//上上上级id
                    if($upper_upper_agent_id>0){
                        $param['upper_upper_agent_id']=$upper_upper_agent_id;
                        $param['upper_agent_id']=$upper_agent_id;
                        $param['agent_id']=$agent_id;
                        $param['upper_upper_agent_tel']=$upper_upper_agent_tel;
                        $param['upper_agent_tel']=$upper_agent_tel;
                        $param['agent_tel']=$agent_tel;
                    }else{
                        if($upper_agent_id>0){
                            $param['upper_upper_agent_id']=0;
                            $param['upper_agent_id']=$upper_agent_id;
                            $param['agent_id']=$agent_id;
                            $param['upper_upper_agent_tel']=0;
                            $param['upper_agent_tel']=$upper_agent_tel;
                            $param['agent_tel']=$agent_tel;
                        }else{
                            $param['upper_upper_agent_id']=0;
                            $param['upper_agent_id']=0;
                            $param['agent_id']=$agent_id;
                            $param['upper_upper_agent_tel']=0;
                            $param['upper_agent_tel']=0;
                            $param['agent_tel']=$agent_tel;
                        }
                    }
                }

            }else{

                $param['upper_upper_agent_id']=0;
                $param['upper_agent_id']=0;
                $param['agent_id']=0;
                $param['upper_upper_agent_tel']=0;
                $param['upper_agent_tel']=0;
                $param['agent_tel']=0;
            }
            $Data = M('register');
            $re=$Data->data($param)->add();
            if ($re){
                //  echo '保存成功';
                // $this->success('保存成功', "/admin/Index/index");
                $this->success('添加成功',"/index.php/admin/Index/adduser" );
            }else   {
                //     echo  '保存失败';
                $this->error('添加失败' );

            }

        }else
        {

            $this->display("adduser");
        }
    }

//    public function adduser()
//    {
//        if(IS_POST){
//            //$param['upper_agent_id']=I('upper_agent_id');
//            //$param['agent_id']=I('agent_id');
//            $param['register_name']=I('register_name');
//            //$param['certificate']=I('certificate');
//            $param['tel'] = I('tel');//手机号
//            $param['password'] = md5(I("password"));//密码
//            $param['passwordTwo'] = I("password");//密码
//            //$param['upper_agent_tel']=I('upper_agent_tel');
//            //$param['agent_tel']=I('agent_tel');
//            $param["create_time"]=date('y-m-d h:i:s',time());
//
//
//            $Data = M('register');
//            $re=$Data->data($param)->add();
//            if ($re){
//                //  echo '保存成功';
//                // $this->success('保存成功', "/admin/Index/index");
//                $this->success('添加成功',"/index.php/admin/Index/userlist" );
//            }else   {
//                //     echo  '保存失败';
//                $this->error('添加失败' );
//
//            }
//
//        }else
//        {
//
//            $this->display("adduser");
//        }
//    }

    public function search()
    {
        //搜索
        $select = $_POST['select'];

        $keyword = $_POST['keyword'];
        $select_txt = $_POST['select_name'];
        if($keyword==''){
            $this->error('关键词不能为空');
        }else{
            $param=null;
            $Data=M("register");
            import('ORG.Util.Page');// 导入分页类
            $count = $Data->where( $param)->count();// 查询满足要求的总记录数 $map表示查询条件
            $Page = new \Think\Page($count,20);// 实例化分页类 传入总记录数

            foreach($param as $key=>$val) {
                $Page->parameter  .=  "$key=".urlencode($val)."&";
            }

            $show = $Page->show();// 分页显示输出

            $register = M("register")->where("$select like '%{$keyword}%'")->limit($Page->firstRow.','.$Page->listRows)->select();
            $count =M("register")->where("$select like '%{$keyword}%'")->limit($Page->firstRow.','.$Page->listRows)->count();

            $this->assign("select_txt",$select_txt);
            $this->assign("keyword",$keyword);
            $this->assign("count",$count);
            $this->assign("register",$register);// 赋值数据集
            $this->assign("page",$show);    //赋值分页输出
            $this->assign("param",$param);  // 赋值分页输出

            $this->display("search");
        }
 

    }

    public function pbonus_search()
    {
        //搜索

        $select = $_POST['select'];
        $keyword = $_POST['keyword'];
        $select_txt = $_POST['select_name'];
        if($keyword==''){
            $this->error('关键词不能为空');
        }else{
            $param=null;
            $Data=M("pbonus");
            import('ORG.Util.Page');// 导入分页类
            $count = $Data->where( $param)->count();// 查询满足要求的总记录数 $map表示查询条件
            $Page = new \Think\Page($count,20);// 实例化分页类 传入总记录数

            foreach($param as $key=>$val) {
                $Page->parameter  .=  "$key=".urlencode($val)."&";
            }

            $show = $Page->show();// 分页显示输出
            $id= M("register")->where("$select like '%{$keyword}%'")->getField('id');
            //$register = M("register")->where("$select like '%{$keyword}%'")->limit($Page->firstRow.','.$Page->listRows)->select();
            $count =M("pbonus")->where('uid='.$id)->limit($Page->firstRow.','.$Page->listRows)->count();
            //echo M("pbonus")->getLastSql();die;
            $user= $Data->join('register on pbonus.uid = register.id')->where('uid='.$id)->field('register.tel as utel,pbonus.job_no,pbonus.tel,pbonus.phone,pbonus.recharge,pbonus.moneys,pbonus.ctime')->order('ctime desc')->limit($Page->firstRow.','.$Page->listRows)->select();
            $this->assign("select_txt",$select_txt);
            $this->assign("keyword",$keyword);
            $this->assign("count",$count);
            $this->assign("user",$user);// 赋值数据集
            $this->assign("page",$show);    //赋值分页输出
            $this->assign("param",$param);  // 赋值分页输出

            $this->display("pbonuslist");
        }


    }

    public function search1()
    {
        //搜索

        $select = $_POST['select'];
        $keyword = $_POST['keyword'];
        $select_txt = $_POST['select_name'];
        if($select==''||$keyword==''){
            $this->error('选项或者关键词不能为空');
        }else{
            $param=null;
            $Data=M("withdraw");
            import('ORG.Util.Page');// 导入分页类
            $count = $Data->where( $param)->count();// 查询满足要求的总记录数 $map表示查询条件
            $Page = new \Think\Page($count,20);// 实例化分页类 传入总记录数

            foreach($param as $key=>$val) {
                $Page->parameter  .=  "$key=".urlencode($val)."&";
            }

            $show = $Page->show();// 分页显示输出
            $withdraw = M("withdraw")->join('register on withdraw.uid=register.id')->where("$select like '%{$keyword}%'")->limit($Page->firstRow.','.$Page->listRows)->select();
            //var_dump($withdraw);
            $count =M("withdraw")->where("$select like '%{$keyword}%'")->limit($Page->firstRow.','.$Page->listRows)->count();
            //var_dump($count);die;


            $this->assign("select_txt",$select_txt);
            $this->assign("keyword",$keyword);
            $this->assign("count",$count);
            $this->assign("withdraw",$withdraw);// 赋值数据集
            $this->assign("page",$show);    //赋值分页输出
            $this->assign("param",$param);  // 赋值分页输出

            $this->display("search1"); 
        }
        

    }
    
    public function search2()
    {
        //搜索

        $select = $_POST['select'];
        $keyword = $_POST['keyword'];
        $select_txt = $_POST['select_name'];
        //var_dump($select);
        //var_dump($keyword);
        //var_dump($select_txt);
        if($keyword==''){
            $this->error('关键词不能为空');
        }else{
            $param=null;
            $Data=M("commission");
            import('ORG.Util.Page');// 导入分页类
            $count = $Data->where( $param)->count();// 查询满足要求的总记录数 $map表示查询条件
            $Page = new \Think\Page($count,20);// 实例化分页类 传入总记录数

            foreach($param as $key=>$val) {
                $Page->parameter  .=  "$key=".urlencode($val)."&";
            }

            $show = $Page->show();// 分页显示输出
            $commission = M("commission")->where("$select like '%{$keyword}%'")->limit($Page->firstRow.','.$Page->listRows)->select();
            //var_dump($commission);die;
            $count =M("commission")->where("$select like '%{$keyword}%'")->limit($Page->firstRow.','.$Page->listRows)->count();
            //var_dump($count);die;


            $this->assign("select_txt",$select_txt);
            $this->assign("keyword",$keyword);
            $this->assign("count",$count);
            $this->assign("commission",$commission);// 赋值数据集
            $this->assign("page",$show);    //赋值分页输出
            $this->assign("param",$param);  // 赋值分页输出

            $this->display("search2");
        }
        

    }

	
	
	public function reward_search(){
            if($_POST){
                $tel=I('tel');
                $date=I('date');
				//var_dump($date);die;
                if($tel==''||$date==''){
                    $this->error('手机号或者日期不能为空');
                }else{
                    $bind= M("register")->where("tel=".$tel)->find();
                    $bind_id=$bind['bind_id'];
                    $id=$bind['id'];
					$length= strlen($date);
					//var_dump($length);die;
					if($length == 10){
						$today = date($date. ' 12:00:01');
						$yestoday=date('Y-m-d 12:00:00',strtotime("$date -1 days"));
                        $commission = $this->get_day_money($id,$yestoday,$today);//获取昨日工资奖金
						$data['time'] = array(array('EGT', $yestoday), array('ELT', $today));
					}else{
						$month=$date;
						$data['time']=array('like',"$month%");
					}

                    $ress=M("register")->where('id='.$id)->find();
                    if($ress){
                        $commissions=$ress['commissions'];
                        //$commission=$ress['commission'];
                    }

                    if($tel!=''&&$date!=''){
                        $team1=M("bonus")->join('register on bonus.mid=register.id')->where('uid='.$id.' and remark=1')->where($data)->order('time')->select();
                        $info1=M("bonus")->field('tel,sum(TCJE) as TCJE')->where('uid='.$id.' and remark=1')->where($data)->group('tel')->select();


                        $team2=M("bonus")->join('register on bonus.mid=register.id')->where('uid='.$id.' and remark=2')->where($data)->order('time')->select();
                        $info2=M("bonus")->field('tel,sum(TCJE) as TCJE')->where('uid='.$id.' and remark=2')->where($data)->group('tel')->select();

                        $team3=M("bonus")->join('register on bonus.mid=register.id')->where('uid='.$id.' and remark=3')->where($data)->order('time')->select();
                        $info3=M("bonus")->field('tel,sum(TCJE) as TCJE')->where('uid='.$id.' and remark=3')->where($data)->group('tel')->select();
                    }elseif($tel!=''&&$date==''){
                        $team1=M("bonus")->join('register on bonus.mid=register.id')->where('uid='.$id.' and remark=1')->order('time')->select();
                        $info1=M("bonus")->field('tel,sum(TCJE) as TCJE')->where('uid='.$id.' and remark=1')->group('tel')->select();
                        $team2=M("bonus")->join('register on bonus.mid=register.id')->where('uid='.$id.' and remark=2')->order('time')->select();
                        $info2=M("bonus")->field('tel,sum(TCJE) as TCJE')->where('uid='.$id.' and remark=2')->group('tel')->select();

                        $team3=M("bonus")->join('register on bonus.mid=register.id')->where('uid='.$id.' and remark=3')->order('time')->select();
                        $info3=M("bonus")->field('tel,sum(TCJE) as TCJE')->where('uid='.$id.' and remark=3')->group('tel')->select();
                    }
                }

//                $money1 = 0;
//                foreach ($team1 as $key=> $value) {
//                    $money1 += $value['money'];
//                }
//                $money2 = 0;
//                foreach ($team2 as $key=> $value) {
//                    $money2 += $value['money'];
//                }
//                $money3 = 0;
//                foreach ($team3 as $key=> $value) {
//                    $money3 += $value['money'];
//                }
//
//                $money=$money1+$money2+$money3;
                
            }
            $this->assign('tel',$tel);
            $this->assign('date',$date);
            //$this->assign('money',$money);
            $this->assign('commission',$commission);
            $this->assign('commissions',$commissions);
            $this->assign('team1',$team1);
            $this->assign('team2',$team2);
            $this->assign('team3',$team3);
            $this->assign('info1',$info1);
            $this->assign('info2',$info2);
            $this->assign('info3',$info3);
            $this->display('rewardlist');
    }

    function get_day_money($id,$beginYesterday,$endYesterday){

        $data['time'] = array('between',array($beginYesterday,$endYesterday));
        $commission = M("bonus")->where('uid='.$id)->where($data)->sum('money');
        return $commission;
    }

    //用户列表
    public function userlist()
    {
        $param=null;
        $Data = M('register'); // 实例化Data数据对象  date 是你的表名
        import('ORG.Util.Page');// 导入分页类
        $count = $Data->where( $param)->count();// 查询满足要求的总记录数 $map表示查询条件
        $Page = new \Think\Page($count,20);// 实例化分页类 传入总记录数

        foreach($param as $key=>$val) {
            $Page->parameter  .=  "$key=".urlencode($val)."&";
        }

        $show = $Page->show();// 分页显示输出
        // 进行分页数据查询
        $count = M("register")->count();
        $user= $Data->where($param)->order('create_time desc')->limit($Page->firstRow.','.$Page->listRows)->select(); // $Page->firstRow 起始条数 $Page->listRows 获取多少条
        $this->assign("count",$count);
        $this->assign('user',$user);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('param',$param);// 赋值分页输出


        $this->display("userlist");
    }

    //充值奖金列表
    public function pbonuslist(){
        $param=null;
        //echo 123;die;
        $Data = M('pbonus'); // 实例化Data数据对象  date 是你的表名
        import('ORG.Util.Page');// 导入分页类
        $count = $Data->where( $param)->count();// 查询满足要求的总记录数 $map表示查询条件
        $Page = new \Think\Page($count,20);// 实例化分页类 传入总记录数
        foreach($param as $key=>$val) {
            $Page->parameter  .=  "$key=".urlencode($val)."&";
        }

        $show = $Page->show();// 分页显示输出
        // 进行分页数据查询
        $count = M("pbonus")->count();
        $user= $Data->join('register on pbonus.uid = register.id')->where($param)->field('register.tel as utel,pbonus.tel,pbonus.job_no,pbonus.phone,pbonus.recharge,pbonus.moneys,pbonus.ctime,pbonus.recharge_no')->order('ctime desc')->limit($Page->firstRow.','.$Page->listRows)->select(); // $Page->firstRow 起始条数 $Page->listRows 获取多少条
        $this->assign("count",$count);
        $this->assign('user',$user);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('param',$param);// 赋值分页输出


        $this->display("pbonuslist");
    }

    //提现明细列表
    public function withdrawlist()
    {
        $param=null;

        $Data = M('withdraw'); // 实例化Data数据对象  date 是你的表名
        import('ORG.Util.Page');// 导入分页类
        $count = $Data->where( $param)->count();// 查询满足要求的总记录数 $map表示查询条件
        $Page = new \Think\Page($count,20);// 实例化分页类 传入总记录数

        foreach($param as $key=>$val) {
            $Page->parameter  .=  "$key=".urlencode($val)."&";
        }

        $show = $Page->show();// 分页显示输出
        // 进行分页数据查询
        $count = M("withdraw")->count();
        $user= $Data->join('register on withdraw.uid=register.id')->where($param)->order('mid')->limit($Page->firstRow.','.$Page->listRows)->select(); // $Page->firstRow 起始条数 $Page->listRows 获取多少条
        //var_dump($user);die;
        $this->assign("count",$count);
        $this->assign('user',$user);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('param',$param);// 赋值分页输出


        $this->display("withdrawlist");
    }

    //奖金池明细列表
    public function commissionlist()
    {
        $param=null;

        $Data = M('commission'); // 实例化Data数据对象  date 是你的表名
        import('ORG.Util.Page');// 导入分页类
        $count = $Data->where($param)->count();// 查询满足要求的总记录数 $map表示查询条件
        $money = $Data->where($param)->sum('money');
        $Page = new \Think\Page($count,20);// 实例化分页类 传入总记录数

        foreach($param as $key=>$val) {
            $Page->parameter  .=  "$key=".urlencode($val)."&";
        }

        $show = $Page->show();// 分页显示输出
        // 进行分页数据查询
        $count = M("commission")->count();
        $user= $Data->where($param)->order('time desc')->limit($Page->firstRow.','.$Page->listRows)->select(); // $Page->firstRow 起始条数 $Page->listRows 获取多少条
        //var_dump($user);die;
        $this->assign("count",$count);
        $this->assign('money',$money);
        $this->assign('user',$user);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('param',$param);// 赋值分页输出


        $this->display("commissionlist");
    }

    public function rewardlist(){

        $this->display('rewardlist');

    }
	
	    public function bonuscollect(){
        $this->display('bonuscollect');
    }

  public function bonus_search(){
        $select = $_POST['select'];
        $startdate = I('date');
        $enddate = I('date1');
        session('date',$startdate);
        session('date1',$enddate);
        $type = 1;
        $startdate = date($startdate. ' 12:00:01');
        $enddate = date( 'Y-m-d 12:00:00',strtotime("$enddate +1 days"));
        $list_wages=jiesuan($select,$startdate,$enddate,$type);
        $zmoneys = array_sum(array_map(function($val){return $val['zmoney'];}, $list_wages));
        S('list_wages', $list_wages);
        S('zmoenys', $zmoneys);
        S('type', $type);
        $this->assign('zmoneys',$zmoneys);
        $this->assign('list_wages',$list_wages);
        $this->display('bonuscollect');
    }

    public function pbonuscollect(){
        $this->display('pbonuscollect');
    }


    public function pbonuscollect_search(){
        $select = $_POST['select'];
        $startdate = I('date');
        $enddate = I('date1');
        session('date2',$startdate);
        session('date3',$enddate);
        $type = 2;
        $startdate = date($startdate. ' 00:00:01');
        $enddate = date($enddate. ' 23:59:59');
        $list_wages=jiesuan($select,$startdate,$enddate,$type);
        $zmoneys = array_sum(array_map(function($val){return $val['zmoney'];}, $list_wages));
        $this->assign('zmoneys',$zmoneys);
        S('list_wages', $list_wages);
        S('type', $type);

        $this->assign('list_wages',$list_wages);
        $this->display('pbonuscollect');
    }
	
	    //充值奖金操作记录
    public function recharge_record(){
        $param=null;
        //echo 123;die;
        $Data = M('pbonus'); // 实例化Data数据对象  date 是你的表名
        import('ORG.Util.Page');// 导入分页类
        $count = $Data->where( $param)->count();// 查询满足要求的总记录数 $map表示查询条件
        $Page = new \Think\Page($count,20);// 实例化分页类 传入总记录数
        foreach($param as $key=>$val) {
            $Page->parameter  .=  "$key=".urlencode($val)."&";
        }

        $show = $Page->show();// 分页显示输出
        // 进行分页数据查询
        $count = M("pbonus")->count();
        $user= $Data->join('register on pbonus.uid = register.id')->where($param)->field('register.tel as utel,pbonus.tel,pbonus.job_no,pbonus.phone,pbonus.recharge,pbonus.moneys,pbonus.ctime,pbonus.recharge_no')->group('pbonus.ctime')->order('ctime desc')->limit($Page->firstRow.','.$Page->listRows)->select(); // $Page->firstRow 起始条数 $Page->listRows 获取多少条
        $this->assign("count",$count);
        $this->assign('user',$user);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('param',$param);// 赋值分页输出
        $this->display("rechargerecord");
    }

    public function record_search(){
        $select = $_POST['select'];
        $keyword = $_POST['keyword'];
        $select_txt = $_POST['select_name'];
        if($keyword==''){
            $this->error('关键词不能为空');
        }else{
            $param=null;
            $Data=M("pbonus");
            import('ORG.Util.Page');// 导入分页类
            $count = $Data->where( $param)->count();// 查询满足要求的总记录数 $map表示查询条件
            $Page = new \Think\Page($count,20);// 实例化分页类 传入总记录数

            foreach($param as $key=>$val) {
                $Page->parameter  .=  "$key=".urlencode($val)."&";
            }

            $show = $Page->show();// 分页显示输出
//            $count = M("pbonus")->where("$select like '%{$keyword}%'")->count();
//            var_dump($count);die;
            $user= $Data->join('register on pbonus.uid = register.id')
                ->where($param)->field('register.tel as utel,pbonus.tel,pbonus.job_no,pbonus.phone,pbonus.recharge,pbonus.moneys,pbonus.ctime,pbonus.recharge_no')
                ->where("$select like '%{$keyword}%'")
                ->group('pbonus.ctime')
                ->order('ctime desc')
                ->limit($Page->firstRow.','.$Page->listRows)
                ->select();
            $count = count($user);
            //var_dump($user);die;
            $this->assign("select_txt",$select_txt);
            $this->assign("keyword",$keyword);
            $this->assign("count",$count);
            $this->assign("user",$user);// 赋值数据集
            $this->assign("page",$show);    //赋值分页输出
            $this->assign("param",$param);  // 赋值分页输出

            $this->display("rechargerecord");
        }
    }

    //删除充值记录
    public function delrecharge(){
        $ctime = I('ctime');
        $res = M('pbonus')->where('ctime='.$ctime)->delete();
        if($res){
            $this->ajaxReturn(array('status'=>1,'info'=>'删除成功'));
        }
    }

    //修改用户信息
    public function edituser()
    {
        if(IS_POST){
            $id=$_GET["id"];
            $param['upper_agent_id']=I('upper_agent_id');
            $param['agent_id']=I('agent_id');
            $param['register_name']=I('register_name');
            $param['certificate']=I('certificate');
            $param['tel'] = I('tel');//手机号
            $param['password'] = md5(I("password"));//密码
            $param['passwordTwo'] = I("password");//密码
            $param['upper_agent_tel']=I('upper_agent_tel');
            $param['agent_tel']=I('agent_tel');
            $param["create_time"]=date('y-m-d h:i:s',time());


            $Data = M('register');
            $re=$Data->where("id={$id}")->data($param)->save();
            if ( $re ){
                //  echo '保存成功';
                // $this->success('保存成功', "/admin/Index/index");
                $this->success('修改成功',"/index.php/admin/Index/userlist" );
            }else   {
                //     echo  '保存失败';
                $this->error('修改失败' );

            }

        }else   {
            $id=I("id");

            $Data = M('register');
            $user = $Data->where("id = {$id}")->find();
            //var_dump($user['passwordtwo']);die;

            $this->assign('user',$user);// 赋值数据集
            $this->display("edituser");
        }


    }

    //用户通过审核
    public function isok(){
        if($_GET){
            $id=I("id");
            $data['isok'] = 1;
            $res = M('register')->where("id = {$id}")->save($data);
            if($res){
                $this->success('审核已通过',"/index.php/admin/Index/userlist" );
            }     
        }
    }

    //用户驳回审核
    public function isnotok(){
        if($_GET){
            $id=I("id");
            $data['isok'] = 0;
            $res = M('register')->where("id = {$id}")->save($data);
            if($res){
                $this->success('审核已驳回',"/index.php/admin/Index/userlist" );
            }     
        }
    }

     //用户解锁
    public function open(){
        if($_GET){
            $id=I("id");
            $data['lock'] = 1;
            $res = M('register')->where("id = {$id}")->save($data);
            if($res){
                $this->success('该用户已经解锁',"/index.php/admin/Index/userlist" );
            }     
        }
    }

    //用户锁定
    public function lock(){
        if($_GET){
            $id=I("id");
            $data['lock'] = 0;
            $res = M('register')->where("id = {$id}")->save($data);
            if($res){
                $this->success('该用户已经锁定',"/index.php/admin/Index/userlist" );
            }     
        }
    }

    //开通前台充值权限
    public function auth(){
        if($_GET){
            $id = I('id');
            $data['auth'] = 1;
            $res =  M('register')->where("id = {$id}")->save($data);
            if($res){
                $this->success('该用户已经开通前台充值权限',"/index.php/admin/Index/userlist");
            }
        }
    }

    //关闭前台充值权限

    public function noauth(){
        if($_GET){
            $id = I('id');
            $data['auth'] = 0;
            $res =  M('register')->where("id = {$id}")->save($data);
            if($res){
                $this->success('该用户已经关闭前台充值权限',"/index.php/admin/Index/userlist");
            }
        }
    }

     //提现通过审核
    public function status_ok(){
        if($_GET){
            $id=I("id");
            $mid=I("mid");
            $money=I("money");
            //var_dump($id);die;
            $data['status'] = 1;
            $res = M('withdraw')->where("mid = {$mid}")->save($data);
            //var_dump($res);die;
            if($res){
                M('register')->where('id='.$id)->setDec('commissions',$money);
                $data['dotime']=date('Y-m-d H:i:s',time());
                M('withdraw')->where('mid='.$mid)->save($data);
                $this->success('审核已通过',"/index.php/admin/Index/withdrawlist" );  
            }     
        }
    }

    //提现未通过审核
    public function status_notok(){
        if($_GET){
            $id=I("id");
            $data['status'] = 0;
            $res = M('withdraw')->where("id = {$id}")->save($data);
            if($res){
                $this->success('审核未通过',"/index.php/admin/Index/withdrawlist" );
            }     
        }
    }

    //删除用户
    public function deluser()
    {
        $param['id']=I('id');

        $Data = M('register');
        $list = $Data->where($param)->delete();
        if ($list)  {
            $this->success('删除成功',"/index.php/admin/Index/userlist" );

        }else   {
            $this->error('删除失败' );

        }
    }

    //删除该条明细
    public function deldraw()
    {
        $param['id']=I('id');

        $Data = M('withdraw');
        $list = $Data->where($param)->delete();
        if ($list)  {
            $this->success('删除成功',"/index.php/admin/Index/withdrawlist" );

        }else   {
            $this->error('删除失败' );

        }
    }

    //删除该条奖金入账明细
    public function delcommission()
    {
        $param['id']=I('id');

        $Data = M('commission');
        $list = $Data->where($param)->delete();
        if ($list)  {
            $this->success('删除成功',"/index.php/admin/Index/commissionlist" );

        }else   {
            $this->error('删除失败' );

        }
    }
    //验证身份证号合法性
    public function check()
    {
        $id_number = $_POST['id_number'];
        $re = validate_identity($id_number);

        if ($re == null) {
            echo "2";
        }
    }
    public function checkid()
    {
        $certificate = $_POST['certificate'];
        $re = validate_identity($certificate);

        if ($re == null) {
            echo "1";
        }
    }
    public function change()
    {
        $tel = $_POST['tel'];
        if ($tel != null) {
            $result = M("register")->where("tel={$tel}")->find();
            if ($result) {
                echo "2";
            }
        }
    }
    //二维码列表
    public function codelist()
    {
        $param=null;

        $Data = M('code'); // 实例化Data数据对象  date 是你的表名
        import('ORG.Util.Page');// 导入分页类
        $count = $Data->where( $param)->count();// 查询满足要求的总记录数 $map表示查询条件
        $Page = new \Think\Page($count,20);// 实例化分页类 传入总记录数

        foreach($param as $key=>$val) {
            $Page->parameter  .=  "$key=".urlencode($val)."&";
        }

        $show = $Page->show();// 分页显示输出
        // 进行分页数据查询
        $codelist= $Data->where($param)->order('create_time desc')->limit($Page->firstRow.','.$Page->listRows)->select(); // $Page->firstRow 起始条数 $Page->listRows 获取多少条
        $this->assign('codelist',$codelist);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('param',$param);// 赋值分页输出


        $this->display("codelist");
    }
    //删除二维码
    public function delcode()
    {
        $param['id']=I('id');
        $Data = M('code');
        $list = $Data->where($param)->delete();
        if ($list)  {
            $this->success('删除成功',"/index.php/admin/Index/codelist" );

        }else   {
            $this->error('删除失败' );

        }
    }

    //工资奖金等级列表
    public function ratiolist()
    {

        $param=null;

        $Data = M('ratio'); // 实例化Data数据对象  date 是你的表名
        import('ORG.Util.Page');// 导入分页类
        $count = $Data->where( $param)->count();// 查询满足要求的总记录数 $map表示查询条件
        $Page = new \Think\Page($count,20);// 实例化分页类 传入总记录数

        foreach($param as $key=>$val) {
            $Page->parameter  .=  "$key=".urlencode($val)."&";
        }

        $show = $Page->show();// 分页显示输出
        // 进行分页数据查询
        $ratio= $Data->where($param)->order('id')->limit($Page->firstRow.','.$Page->listRows)->find(); // $Page->firstRow 起始条数 $Page->listRows 获取多少条
        $this->assign('ratio',$ratio);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('param',$param);// 赋值分页输出


        $this->display("ratiolist");
    }

    //产品奖金等级列表
    public function pratiolist()
    {

        $param=null;

        $Data = M('pratio'); // 实例化Data数据对象  date 是你的表名
        import('ORG.Util.Page');// 导入分页类
        $count = $Data->where( $param)->count();// 查询满足要求的总记录数 $map表示查询条件
        $Page = new \Think\Page($count,20);// 实例化分页类 传入总记录数

        foreach($param as $key=>$val) {
            $Page->parameter  .=  "$key=".urlencode($val)."&";
        }

        $show = $Page->show();// 分页显示输出
        // 进行分页数据查询
        $ratio= $Data->where($param)->order('id')->limit($Page->firstRow.','.$Page->listRows)->find(); // $Page->firstRow 起始条数 $Page->listRows 获取多少条
        $this->assign('ratio',$ratio);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('param',$param);// 赋值分页输出


        $this->display("pratiolist");
    }
    //修改工资奖金数目
    public function editratio()
    {

        if(IS_POST){
            $id=$_GET["id"];
            $param['agent_ratio']=I('agent_ratio');
            $param['upper_agent_ratio']=I('upper_agent_ratio');
            $param['upper_upper_agent_ratio']=I('upper_upper_agent_ratio');
            //var_dump($param['upper_upper_agent_ratio']);die;
            //var_dump($id);die;

            $Data = M('ratio');
            $re=$Data->where("id={$id}")->data($param)->save();
            //var_dump(M('ratio')->getlastsql());die;
            if ($re){
                //  echo '保存成功';
                // $this->success('保存成功', "/admin/Index/index");
                $this->success('修改成功',"/index.php/admin/Index/ratiolist" );
            }else   {
                //     echo  '保存失败';
                $this->error('修改失败' );

            }

        }else   {
            $id=I("id");

            $Data = M('ratio');
            $ratio = $Data->where("id = {$id}")->find();

            $this->assign('ratio',$ratio);// 赋值数据集
            $this->display("editratio");
        }
    }

    //修改产品奖金数目
    public function editpratio()
    {

        if(IS_POST){

            $id=$_GET["id"];
            $param['pratio']=I('pratio');
            $param['agent_pratio']=I('agent_pratio');
            $param['upper_agent_pratio']=I('upper_agent_pratio');
            $param['upper_upper_agent_pratio']=I('upper_upper_agent_pratio');
            //var_dump($param['upper_upper_agent_ratio']);die;
            //var_dump($id);die;

            $Data = M('pratio');
            $re=$Data->where("id={$id}")->data($param)->save();
            if ($re){

                $this->success('修改成功',"/index.php/admin/Index/pratiolist" );
            }else   {
                $this->error('修改失败' );

            }

        }else   {
            $id=I("id");

            $Data = M('pratio');
            $ratio = $Data->where("id = {$id}")->find();

            $this->assign('ratio',$ratio);// 赋值数据集
            $this->display("editpratio");
        }
    }

    //删除工资奖金数
    public function delratio()
    {
        $param['id']=I('id');
        $Data = M('ratio');
        $list = $Data->where($param)->delete();
        if ($list)  {
            $this->success('删除成功',"/index.php/admin/Index/ratiolist" );

        }else   {
            $this->error('删除失败' );

        }
    }

    //删除工资奖金数
    public function delpratio()
    {
        $param['id']=I('id');
        $Data = M('pratio');
        $list = $Data->where($param)->delete();
        if ($list)  {
            $this->success('删除成功',"/index.php/admin/Index/pratiolist" );

        }else   {
            $this->error('删除失败' );

        }
    }
    
    //用户绑定
    public function bind(){  
        if (IS_AJAX) {
            $id=I('id');
            $data['bind_did'] = I('bind_did');
            $data['bind_id'] = I('bind_id');
            $data['bind_name'] = I('bind_name');
            $user=M("register");
            $res=$user->where($data)->select();
            //$this->ajaxReturn($res);
            if (!$res) {
                $ress=$user->where("id='".$id."'")->save($data);
                if ($ress) {
                    $data=array(
                    'status' => 1,
                    "info"=>"员工绑定成功"
                    );
                    $this->ajaxReturn($data);
                }
            } else {
                 $data=array(
                    'status' => 0,
                    "info"=>"该员工已经被绑定过，请选择其他员工绑定"
                );
                $this->ajaxReturn($data);
            }
        } else {
            $id=I('id');
            $res=M("register")->field('bind_id,bind_did')->where('id ='.$id)->find();
            $this->assign('res',$res);
            $this->assign('id',$id);
            $this->display();       
        }
    }
}